//
//  SleepTemperatureCell.swift
//  DaRings
//
//  Created by 尹琼 on 2022/12/19.
//

import UIKit
import MYModule
import Charts
import RxCocoa
import RxSwift

class SleepTemperatureCell: SleepBaseCell {
    
    let titLabel = Label()
    
    let descLabel = Label()
    
    let temperatureLabel = Label()
    
    let unitLabel = Label()
    
    let barChartView = DFBarChartView()
    
    let descriptionView = ExPlainView()
    
    override func makeUI() {
        super.makeUI()
        
        titLabel.text = "temperature_title".language
        titLabel.font = Fonts.boldSystemFont(ofSize: 14.0)
        titLabel.textColor = UIColor.assist_222222
        
        descLabel.numberOfLines = 0
        descLabel.text = "temperature_sub_title".language
        descLabel.font = Fonts.systemFont(ofSize: 12.0)
        descLabel.textColor = UIColor.assist_D0D0D0
        
        temperatureLabel.font = Fonts.boldSystemFont(ofSize: 44.0)
        temperatureLabel.textColor = UIColor.assist_222222
        
        unitLabel.text = "°C"
        unitLabel.font = Fonts.systemFont(ofSize: 12.0)
        unitLabel.textColor = UIColor.assist_222222
        
        
        descriptionView.updateData(textColor: UIColor.Temperature.theme, title: "temperature_known".language, explain: "temperature_description_content".language,
                                   explainTextColor: UIColor.Temperature.theme.withAlphaComponent(0.6),
                                   bgColor: UIColor.Temperature.theme)
        
        let barConfig = DFBarChartViewConfig(xAxisLabelFont: Fonts.systemFont(ofSize: 10), xAxisLabelTextColor: UIColor.assist_D0D0D0, xAxisLineColor: .clear)
    
        barChartView.setConfig(barConfig)

        barChartView.rightAxis.labelTextColor = UIColor.assist_D0D0D0
        barChartView.rightAxis.axisLineColor = .clear
        barChartView.rightAxis.enabled = true
        barChartView.rightAxis.drawGridLinesEnabled = true
        barChartView.rightAxis.gridLineDashLengths = [3.0, 3.0] //线段类型
        barChartView.rightAxis.gridColor = UIColor.assist_D0D0D0 //网格线颜色
        barChartView.rightAxis.gridAntialiasEnabled = true//开启抗锯齿
      
        barChartView.rightAxis.forceLabelsEnabled = false
        
        
        resetBarCustomAxisMax()
        
        barChartView.rightAxis.valueFormatter = SleepTemperatureRightAxisValueFormatter()
        
        let stackView = StackView()
        stackView.alignment = .bottom
        stackView.distribution = .equalSpacing
        stackView.spacing = 2.0
        
        stackView.addArrangedSubview(temperatureLabel)
        stackView.addArrangedSubview(unitLabel)
        
        contentView.addSubview(titLabel)
        contentView.addSubview(descLabel)
        contentView.addSubview(stackView)
        contentView.addSubview(barChartView)
        contentView.addSubview(descriptionView)
        
        titLabel.snp.makeConstraints { make in
            make.left.equalTo(20)
            make.top.equalTo(15)
        }
        
        descLabel.snp.makeConstraints { make in
            make.left.equalTo(20)
            make.right.lessThanOrEqualTo(-20)
            make.top.equalTo(titLabel.snp.bottom).offset(10)
        }
        
        stackView.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(descLabel.snp.bottom).offset(13)
        }
        
        barChartView.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(15)
            make.right.equalToSuperview().offset(-15)
            make.top.equalTo(stackView.snp.bottom).offset(10)
            make.height.equalTo(160)
        }
        
        descriptionView.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(15)
            make.right.equalToSuperview().offset(-15)
            make.top.equalTo(barChartView.snp.bottom).offset(20)
            make.bottom.equalToSuperview().offset(-20)
        }
        
        
    }
    
    override func bind(to cellType: SleepViewModel.CellType) {
        super.bind(to: cellType)
        
        guard let viewModel = cellType.viewModel as? SleepTemperatureViewModel else { return }
        
        let disposeBag = DisposeBag()
        
        viewModel.dateRelay.subscribe(onNext: {[weak self] in
            
            let xValueFormatter = DayAxisValueDateFormatter()
            guard let date = $0 else {
                
                xValueFormatter.axisValueArray = ["0", "0", "0", "0", "0", "0", "0"]
                self?.barChartView.xAxis.valueFormatter = xValueFormatter
               
                return
                
            }
            let days: [String] = (0 ..< 7).enumerated().map { (i, int) ->String in
                if i == 0 {
                    return "\(date.day)"
                }
                return "\((date - Double(int).days).day)"
                
            }
            xValueFormatter.axisValueArray = days.reversed()
            self?.barChartView.xAxis.valueFormatter = xValueFormatter
           
    
        }).disposed(by: disposeBag)
        
        viewModel.sevenTemperatureDiffs.subscribe(onNext: {[weak self] in
            
            var color = UIColor.Temperature.theme
            
            guard let model = $0 else {
                color = .clear
                /// 空数据显示
                self?.temperatureLabel.text = "--"
                let emptyData = (0 ..< 7).map { _ in (0.0, 0.0) }
                self?.barChartView.setDoubleLineData(emptyData, lowBarColor: color, highBarColor: color, lowBarHighLichtColor: color, highBarHighLichtColor: color, highlightEnabled: false)
                return
            }
            
            let data: [(low: Double, high: Double)] = model.barModels.map { model ->(low: Double, high: Double) in
                
                return model.chartTuples
            }
            
            if model.isSetChartMaxMinAble {
                /// 默认最大最小值
                self?.setBarCustomAxisMax(max: model.chartMax, min: model.chartMin)
            } else {
                self?.resetBarCustomAxisMax()
            }
        
            self?.barChartView.rightAxis.labelCount = model.chartYAixCount
            self?.barChartView.rightAxis.forceLabelsEnabled = true
            
            self?.temperatureLabel.text = model.todayTemperatureDiffText
            
            self?.barChartView.setDoubleLineData(data, lowBarColor: color, highBarColor: color, lowBarHighLichtColor: color, highBarHighLichtColor: color, highlightEnabled: false)
            
            
        }).disposed(by: disposeBag)
        

    }
    
    func resetBarCustomAxisMax() {
        
        barChartView.rightAxis.resetCustomAxisMax()
        barChartView.rightAxis.resetCustomAxisMin()
        barChartView.leftAxis.resetCustomAxisMin()
        barChartView.leftAxis.resetCustomAxisMax()
        
    }
    
    func setBarCustomAxisMax(max: Double, min: Double) {
        
        barChartView.rightAxis.axisMaximum = max
        barChartView.rightAxis.axisMinimum = min
        barChartView.leftAxis.axisMaximum = max
        barChartView.leftAxis.axisMinimum = min
    }
    

   
}

